<?php

/* * 
 * 活动
 */
namespace Api\Model;
use Think\Model;
class ActivityModel extends Model {

    //映射
    protected $_map = array(

    );

    //获取活动
    public function getActivityById($id) {

        $activity = $this->where(array('activity_id' => $id))->find();

        return $activity;

    }

    //获取活动列表（筛选）
    public function getActivityList($categoryId, $cityFilter, $timeFilter, $extraFilter, $myLocation=array(), $pageNum) {

        $where = "cr.relation_type = " .C("CATEGORY_RELATIONSHIP_TYPE.ACTIVITY"). "
                  AND cr.category_id = " .$categoryId;

        //城市筛选
        if(!isNullOrEmpty($cityFilter)) {
            $where .= " AND a.city = '" .$cityFilter. "'";
        }

        //判断时间筛选类型
        switch ($timeFilter) {
            case C("ACTIVITY_TIME_FILTER.WAITING"):
                $where .= " AND a.status = " .C('ACTIVITY_STATUS.WAITING');
                break;
            case C("ACTIVITY_TIME_FILTER.START"):
                $where .= " AND a.status = " .C('ACTIVITY_STATUS.START');
                break;
            case C("ACTIVITY_TIME_FILTER.WEEK"):
                $where .= " AND a.status != " .C('ACTIVITY_STATUS.VERIFY'). "
                            AND TIMESTAMPDIFF(DAY,a.start_time,NOW()) BETWEEN 0 AND 7";
                break;
            case C("ACTIVITY_TIME_FILTER.MONTH"):
                $where .= " AND a.status != " .C('ACTIVITY_STATUS.VERIFY'). "
                            AND TIMESTAMPDIFF(DAY,a.start_time,NOW()) BETWEEN 0 AND 30";
                break;
            default:
                break;
        }

        //判断其他筛选类型
        if($extraFilter == C("ACTIVITY_EXTRA_FILTER.HOT")) {
            $order = "a.join_count DESC";
        } else {
            $order = "a.create_time ASC";
        }

        $list = $this->alias('a')
            ->join("LEFT JOIN __CATEGORY_RELATIONSHIP__ cr ON a.activity_id = cr.reference_id")
            ->field("a.activity_id, a.activity_name, a.activity_image_url, a.time_string, a.address,
            a.latitude, a.longitude, a.join_count, a.collect_count")
            ->where($where)
            ->order($order)
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        //判断其他筛选类型
        if($extraFilter == C("ACTIVITY_EXTRA_FILTER.DISTANCE")) {
            if(!isNullOrEmpty($list)) {
                foreach ($list as &$item) {
                    $item['distance'] = getDistance($myLocation['latitude'], $myLocation['longitude'], $item['latitude'], $item['longitude']);
                }

                $list = mySort($list,'distance',SORT_ASC,SORT_NUMERIC);
            }
        }

        return $list;

    }

    //获取活动详情
    public function getActivityDetailById($id) {

        $activity = $this
            ->field("activity_id, activity_image_url, activity_name, status, time_string, start_time, end_time, 
            address, sponsor, sponsor_mobile, description")
            ->where(array('activity_id' => $id))
            ->find();

        $activity['category_list'] = D('categoryRelationship')->getCategoryList($activity['activity_id'],C("CATEGORY_RELATIONSHIP_TYPE.ACTIVITY"));

        return $activity;

    }

    //计算活动开始时间
    public function getActivityTimeGap($id) {

        $activity = $this->getActivityById($id);

        if($activity['start_time'] > getCurrentDate()) {
            $diffArray = timeDiff(time(), strtotime($activity['start_time']));
            $diffString = $diffArray['day'] . '天' . $diffArray['hour'] . '小时' . $diffArray['min'] . '分钟';

            return $diffString;
        } else {
            return null;
        }

    }


}

